package com.cango.student.database.mybatis;

import com.cango.student.database.mybatis.mapper.AccountMapper;
import com.cango.student.database.mybatis.usermapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

public class JTAService {
    @Autowired
    private UserMapper userMapper;//操作db_user库
    @Autowired
    private AccountMapper accountMapper;//操作db_account库

    @Transactional
    public void insert() {
        User user = new User();
        user.setName("wangxiaoxiao");
        userMapper.insert(user);

        //    int i = 1 / 0;//模拟异常，spring回滚后，db_user库中user表中也不会插入记录
        Account account = new Account();
        account.setUserId(user.getId());
        account.setMoney(123456789);
        accountMapper.insert(account);
    }
}
